/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.fatecmm.cadastrotimes.servlets;

import br.edu.fatecmm.cadastrotimes.banco.TimesDAO;
import br.edu.fatecmm.cadastrotimes.beans.BnTimes;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author jhonatan
 */
public class SltTimes extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        operacao = request.getParameter("operacao");
        chave = request.getParameter("chave");

        RequestDispatcher rd = null;
        
        if("incluir".equals(operacao))
        {
            rd = request.getRequestDispatcher("/incluirTimes.jsp");
        }
        else if("gravarinclusao".equals(operacao))
        {
            bnTimes = new BnTimes();
            bnTimes.setDs_times(request.getParameter("ds_times"));
            bnTimes.setCd_jogador(Integer.parseInt(request.getParameter("cd_jogador")));
            bnTimes.setCd_tecnico(Integer.parseInt(request.getParameter("cd_tecnico")));
            bnTimes.setCd_pais(Integer.parseInt(request.getParameter("cd_pais")));
            bnTimes.setCd_posicao(Integer.parseInt(request.getParameter("cd_posicao")));
            bnTimes.setCd_liga(Integer.parseInt(request.getParameter("cd_liga")));

            try
            {
                timesDAO = new TimesDAO();
                timesDAO.salvar(bnTimes);

                rd = request.getRequestDispatcher("SltTimes?operacao=listar");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("editar".equals(operacao))
        {
            try
            {
                bnTimes = new BnTimes();
                timesDAO = new TimesDAO();
                bnTimes = timesDAO.procurar(Integer.parseInt(chave));
                HttpSession session = request.getSession(true);
                session.setAttribute("bnTimes", bnTimes);
                rd = request.getRequestDispatcher("/alterarTimes.jsp");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("gravaralteracao".equals(operacao))
        {
            bnTimes = new BnTimes();
            bnTimes.setId_times(Integer.parseInt(request.getParameter("id_times")));
            bnTimes.setDs_times(request.getParameter("ds_times"));
            bnTimes.setCd_jogador(Integer.parseInt(request.getParameter("cd_jogador")));
            bnTimes.setCd_tecnico(Integer.parseInt(request.getParameter("cd_tecnico")));
            bnTimes.setCd_pais(Integer.parseInt(request.getParameter("cd_pais")));
            bnTimes.setCd_posicao(Integer.parseInt(request.getParameter("cd_posicao")));
            bnTimes.setCd_liga(Integer.parseInt(request.getParameter("cd_liga")));
            
            try
            {
                timesDAO = new TimesDAO();
                timesDAO.atualizar(bnTimes);

                rd = request.getRequestDispatcher("SltTimes?operacao=listar");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("listar".equals(operacao))
        {
            try
            {
                timesDAO = new TimesDAO();
                List itens = timesDAO.todos();
                request.setAttribute("itens", itens);
                rd = request.getRequestDispatcher("/timesListar.jsp");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        else if("excluir".equals(operacao))
        {
            try
            {
                bnTimes = new BnTimes();
                timesDAO = new TimesDAO();
                bnTimes.setId_times(Integer.parseInt(chave));
                timesDAO.excluir(bnTimes);
                rd = request.getRequestDispatcher("StlTimes?operacao=listar");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        

        if(rd != null)
            rd.forward(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
    private String operacao;
    private String chave;
    private BnTimes bnTimes;
    private TimesDAO timesDAO;
}
